package com.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.bean.Member;
import com.mall.bean.MemberExample;
import com.mall.dao.MemberMapper;
import com.mall.service.MemberServic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MemberServiceImpl implements MemberServic {

    @Autowired
    MemberMapper memberMapper;
    @Override
    public int insert(Member member) {
        Date date = new Date();
        //设置默认路径
        member.setFile("http://lwd520.oss-cn-zhangjiakou.aliyuncs.com/20220402/b305c17c3c734b6c8908b38f8b7ce50e.jpeg"); //../assets/img/1.jpg
        member.setIntegral(0);
        member.setFlag(1);
        member.setCreated(date);
        member.setUpdated(date);
        member.setMoney(new BigDecimal(0));
        int res = memberMapper.insert(member);
        return res;

    }

    @Override
    public List<Member> queryData(Member member) {
        return null;
    }

    @Override
    public int update(Member member) {

        if (member.getFlag() != 0) {
            member.setFlag(0);
        } else {
            member.setFlag(1);
        }

        int res = memberMapper.updateByPrimaryKeySelective(member);
        return res;
    }

    @Override
    public int deleteById(String id) {
        return 0;
    }

    @Override
    public Member queryDataById(String id) {
        return null;
    }

    @Override
    public PageInfo<Member> queryDataByPage(int currentPage, int pageSize, Member member) {
        PageHelper.startPage(currentPage,pageSize);
        MemberExample memberExample=new MemberExample();
        MemberExample.Criteria memberExampleCriteria = memberExample.createCriteria();
        if (member != null) {
            if (member.getNickname() != null && !"".equals(member.getNickname())) {
                memberExampleCriteria.andNicknameLike("%"+member.getNickname()+"%");
            }
            if (member.getUsername() != null && !"".equals(member.getUsername())) {
                memberExampleCriteria.andUsernameLike("%"+member.getUsername()+"%");
            }
        }
        List<Member> memberList = memberMapper.selectByExample(memberExample);
        PageInfo<Member> memberPageInfo=new PageInfo<>(memberList);
        return memberPageInfo;
    }

    @Override
    public Member existUserName(String username) {
        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andFlagEqualTo(1);
        criteria.andUsernameEqualTo(username);
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() > 0) {
            return members.get(0);
        }
        return null;

    }

    @Override
    public Member queryMemberByPassword(Member member) {
        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andFlagEqualTo(1);

        criteria.andUsernameEqualTo(member.getUsername());
        criteria.andPasswordEqualTo(member.getPassword());

        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() > 0) {
            return members.get(0);
        }
        return null;

    }

    @Override
    public Member getMemberInfoById(String memberId) {
        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andFlagEqualTo(1);
        criteria.andMember_idEqualTo(Integer.parseInt(memberId));
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() > 0) {
            return members.get(0);
        }
        return null;
    }



}
